package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListGoodsController {

    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;
    /*
    http://localhost:8080/purchaseListGoods/save?purchaseNumber=JH1605768306735（进货单号前端生成）
    请求参数：PurchaseList purchaseList, String purchaseListGoodsStr
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    //进货单保存
    @PostMapping("save")
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr,String purchaseNumber){
        return purchaseListGoodsService.save(purchaseList,purchaseListGoodsStr,purchaseNumber);
    }

    /*
    请求URL：http://localhost:8080/purchaseListGoods/list
    请求参数：String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime
    请求方式：POST
    返回值类型：JSON
    返回值：Map<String,Object>
     */
    //进货单列表展示（可条件查询：单据号模糊、供应商、是否付款和日期区间）
    @PostMapping("list")
    public Map<String,Object> list(String purchaseNumber, Integer supplierId, Integer state, String sTime,String eTime){
        List<PurchaseList> list=purchaseListGoodsService.getList(purchaseNumber,supplierId,state,sTime,eTime);
        HashMap<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    /*
    请求URL：http://localhost:8080/purchaseListGoods/goodsList
    请求参数：Integer purchaseListId
    请求方式：POST
    返回值类型：JSON
    返回值：Map<String,Object>
     */
    //查询进货单商品信息
    @PostMapping("goodsList")
    public Map<String,Object> goodsList(Integer purchaseListId){
        List<PurchaseListGoods> list=purchaseListGoodsService.getGoodsList(purchaseListId);
        Map<String, Object> map = new HashMap<>();
        map.put("rows",list);
        return map;
    }

    /*
    请求URL：http://localhost:8080/purchaseListGoods/delete
    请求参数：Integer purchaseListId
    请求方式：POST
    返回值类型：JSON
    返回值：ServiceVO
     */
    //进货单删除
    @PostMapping("delete")
    public ServiceVO delete(Integer purchaseListId){
        if (purchaseListId!=null){
            return purchaseListGoodsService.delete(purchaseListId);
        }else {
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }

    /*
        请求URL：http://localhost:8080/purchaseListGoods/updateState
        请求参数：Integer purchaseListId
        请求方式：POST
        返回值类型：JSON
        返回值：ServiceVO
         */
    //支付结算（修改进货单付款状态）
    @PostMapping("updateState")//从1改为2
    public ServiceVO updateState(Integer purchaseListId){
        return purchaseListGoodsService.updateState(purchaseListId);
    }

    /*
    请求URL：http://localhost:8080/purchaseListGoods/count
    请求参数：String sTime, String eTime ,Integer goodsTypeId, String codeOrName
    请求方式：POST
    返回值类型：JSON
    返回值：String
     */
    //进货统计（可根据商品类别、商品编码或名称进行条件查询）
    @PostMapping("count")
    public List<Map<String,Object>> count(String sTime, String eTime ,Integer goodsTypeId, String codeOrName){
        return purchaseListGoodsService.count(sTime,eTime,goodsTypeId,codeOrName);
    }
}
